﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using ProductKeyDB;

namespace ProductKeysWin
{
    /// <summary>
    /// Interaction logic for NewDialog.xaml
    /// </summary>

    public partial class NewDialog : Window
    {
        public NewDialog()
        {
            InitializeComponent();
        }

        private Product _NewProduct = new Product {  };

        public Product NewProduct
        {
            get 
            {
                foreach (ProductKeyDB.Key code in _KeyCodes)
                {
                    _NewProduct.Keys.Add(code);
                }

                return _NewProduct;
            }
        }

        private Product _OldKey;

        public Product OldKey
        {
            get { return _OldKey; }
            set
            {
                _OldKey = value;
                _NewProduct = new Product { Name = _OldKey.Name};
                foreach (ProductKeyDB.Key code in _OldKey.Keys)
                {
                    _KeyCodes.Add(new ProductKeyDB.Key { KeyCode = code.KeyCode, AllocatedTo = code.AllocatedTo });
                }
            }
        }

        private ObservableCollection<ProductKeyDB.Key> _KeyCodes = new ObservableCollection<ProductKeyDB.Key>();

        protected void WindowLoaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = _NewProduct;
            lstProdKeys.DataContext = _KeyCodes;
            AddEditToolbar.DataContext = _KeyCodes;
        }

        protected void AddKeyCode(object sender, RoutedEventArgs e)
        {
            ProductKeyDB.Key code = new ProductKeyDB.Key { KeyCode = "New Key", AllocatedTo = "Not Allocated" };
            _KeyCodes.Add(code);
            lstProdKeys.SelectedItem = code;
        }

        protected void RemoveKeyCode(object sender, RoutedEventArgs e)
        {
            _KeyCodes.Remove((ProductKeyDB.Key)lstProdKeys.SelectedItem);
        }
    }
}
